﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPAMain数据维护工具V2
{
    public partial class frmAddGD : Form
    {
        SqlTools sqt;

        string strInsertGD_A = "insert into gd_table (gd001,gd002,gd003,gd004,gd005,gd006,gd007,gd008,gd009,gd010,gd020,gd021,gd022,gd025,gd029,gd034,gd036,gd037,gd039,gd040,gd043) values ('@工单号','@开卡日期','DS','系统管理员','002','综合管理室','@型号规格','','','@开卡数量','Y','DS',GETDATE (),'A','@开卡工序','@批号','','','正常工单','','603')";
        string strInsertGD_B = "insert into gd_table (gd001,gd002,gd003,gd004,gd005,gd006,gd007,gd008,gd009,gd010,gd020,gd021,gd022,gd025,gd026,gd029,gd034,gd036,gd037,gd039,gd040,gd043) values ('@工单号','@开卡日期','DS','系统管理员','002','综合管理室','@型号规格','','','@开卡数量','Y','DS',GETDATE (),'B','@来源工单','@开卡工序','@批号','','','正常工单','','603')";
        string strInsertGxjy = "insert into gxjy_table (gxjy001,gxjy002,gxjy003,gxjy004,gxjy005,gxjy006,gxjy007,gxjy008,gxjy016,gxjy018,gxjy023) values ('@工单号','',1,'@型号规格','','只','@工序','@结余数量','@批号','','603')";
        string strSelectMaxGDNo = "select max(gd001) from gd_table where gd001 like '@日期%'";

        public frmAddGD()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int iMaxID=0;
            int iNewID=0;
            string strMaxGDNo = string.Empty;
            string strNewID = string.Empty;
            #region 增加A卡
            if (checkBox1.Checked)
            {
                try
                {
                    //检查信息是否填写完整
                    if (txtSNA.Text.Trim().Equals(string.Empty) ||
                        txtModelA.Text.Trim().Equals(string.Empty) ||
                        txtNumberA.Text.Trim().Equals(string.Empty) ||
                        txtDateA.Text.Trim().Equals(string.Empty) ||
                        txtGXA.Text.Trim().Equals(string.Empty) ||
                        txtJYA.Text.Trim().Equals(string.Empty))
                    {
                        GeneralTools.ShowWarningMsg("信息未填写完整！增加A卡失败！");
                        return;
                    }
                    //增加A卡工单
                    //'@工单号','@开卡日期','@型号规格','@开卡数量','@开卡工序','@批号'
                    //获取最大工单号
                    strMaxGDNo = GeneralTools.GetString(sqt.DoCmdScalarData(strSelectMaxGDNo.Replace("@日期", txtDateA.Text.Trim())), string.Empty);
                    if (!strMaxGDNo.Equals(string.Empty) && strMaxGDNo.Length >= 12)
                    {
                        iMaxID = GeneralTools.GetInt(strMaxGDNo.Substring(8, 4), 0);
                    }
                    else
                    {
                        iMaxID = 0;
                    }
                    iNewID = iMaxID + 1;
                    strNewID = txtDateA.Text.Trim() + iNewID.ToString("0000");
                    if (sqt.DoCmdExecuteNonQuery(strInsertGD_A.Replace("@工单号", strNewID)
                                                                                                .Replace("@开卡日期", txtDateA.Text.Trim())
                                                                                                .Replace("@型号规格", txtModelA.Text.Trim())
                                                                                                .Replace("@开卡数量", txtNumberA.Text.Trim())
                                                                                                .Replace("@开卡工序", txtGXA.Text.Trim())
                                                                                                .Replace("@批号", txtSNA.Text.Trim())) == 1)
                    {
                        label11.Text = "增加新A卡成功！工单号：" + strNewID;
                        txtGDNoA.Text = strNewID;
                    }
                    else
                    {
                        throw new Exception("增加新A卡失败！工单号：" + strNewID);
                    }
                    //增加A卡结余
                    if (sqt.DoCmdExecuteNonQuery(strInsertGxjy.Replace("@工单号", strNewID)
                                                                                                .Replace("@型号规格", txtModelA.Text.Trim())
                                                                                                .Replace("@工序", txtGXA.Text.Trim())
                                                                                                .Replace("@结余数量", txtJYA.Text.Trim())
                                                                                                .Replace("@批号", txtSNA.Text.Trim())) == 1)
                    {
                        label11.Text = "增加A卡结余成功！工单号：" + strNewID;
                    }
                    else
                    {
                        throw new Exception("增加A卡结余失败！工单号：" + strNewID);
                    }
                }
                catch(Exception ex)
                {
                    GeneralTools.ShowErrorMsg(ex.Message);
                    return;
                }
            }
            #endregion
            #region 增加B卡
            if (checkBox2.Checked)
            {
                try
                {
                    //检查信息是否填写完整
                    if (txtSNB.Text.Trim().Equals(string.Empty) ||
                        txtModelB.Text.Trim().Equals(string.Empty) ||
                        txtNumberB.Text.Trim().Equals(string.Empty) ||
                        txtDateB.Text.Trim().Equals(string.Empty) ||
                        txtGXB.Text.Trim().Equals(string.Empty) ||
                        txtJYB.Text.Trim().Equals(string.Empty))
                    {
                        GeneralTools.ShowWarningMsg("信息未填写完整！增加A卡失败！");
                        return;
                    }
                    //增加A卡工单
                    //'@工单号','@开卡日期','@型号规格','@开卡数量','@开卡工序','@批号'
                    //获取最大工单号
                    strMaxGDNo = GeneralTools.GetString(sqt.DoCmdScalarData(strSelectMaxGDNo.Replace("@日期", txtDateB.Text.Trim())), string.Empty);
                    if (!strMaxGDNo.Equals(string.Empty) && strMaxGDNo.Length >= 12)
                    {
                        iMaxID = GeneralTools.GetInt(strMaxGDNo.Substring(8, 4), 0);
                    }
                    else
                    {
                        iMaxID = 0;
                    }
                    iNewID = iMaxID + 1;
                    strNewID = txtDateB.Text.Trim() + iNewID.ToString("0000");
                    string strNoA = string.Empty;
                    if (checkBox1.Checked)
                    {
                        strNoA = txtGDNoA.Text.Trim();
                    }
                    if (sqt.DoCmdExecuteNonQuery(strInsertGD_B.Replace("@工单号", strNewID)
                                                                                                .Replace("@开卡日期", txtDateB.Text.Trim())
                                                                                                .Replace("@型号规格", txtModelB.Text.Trim())
                                                                                                .Replace("@开卡数量", txtNumberB.Text.Trim())
                                                                                                .Replace("@开卡工序", txtGXB.Text.Trim())
                                                                                                .Replace("@来源工单", strNoA )
                                                                                                .Replace("@批号", txtSNB.Text.Trim())) == 1)
                    {
                        label11.Text = "增加新B卡成功！工单号：" + strNewID;
                        txtGDNoB.Text = strNewID;
                    }
                    else
                    {
                        throw new Exception("增加新B卡失败！工单号：" + strNewID);
                    }
                    //增加B卡结余
                    if (sqt.DoCmdExecuteNonQuery(strInsertGxjy.Replace("@工单号", strNewID)
                                                                                                .Replace("@型号规格", txtModelB.Text.Trim())
                                                                                                .Replace("@工序", txtGXB.Text.Trim())
                                                                                                .Replace("@结余数量", txtJYB.Text.Trim())
                                                                                                .Replace("@批号", txtSNB.Text.Trim())) == 1)
                    {
                        label11.Text = "增加B卡结余成功！工单号：" + strNewID;
                    }
                    else
                    {
                        throw new Exception("增加B卡结余失败！工单号：" + strNewID);
                    }
                }
                catch (Exception ex)
                {
                    GeneralTools.ShowErrorMsg(ex.Message);
                    return;
                }
            }
            #endregion
        }

        private void frmAddGD_Load(object sender, EventArgs e)
        {
            sqt=new SqlTools (GeneralTools.GetConnectionString (TPAMain数据维护工具V2.Properties .Settings.Default.Host ,TPAMain数据维护工具V2 .Properties.Settings.Default.DBName ));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = checkBox2.Checked;
        }
    }
}
